package com.yinp.proapp.module.task.activity;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.viewbinding.ViewBinding;

import com.yinp.proapp.base.App;
import com.yinp.proapp.base.activity.AppBaseActivity;
import com.yinp.proapp.constant.Constant;
import com.yinp.proapp.databinding.ActivityAddUndeterminedBinding;
import com.yinp.proapp.databinding.DialogTipBinding;
import com.yinp.proapp.greendao.DaoUtils;
import com.yinp.proapp.module.task.bean.LocaleTaskBean;
import com.yinp.proapp.utils.StatusBarUtil;
import com.yinp.tools.fragment_dialog.BaseDialogFragment;
import com.yinp.tools.fragment_dialog.CommonDialogFragment;
import com.yinp.tools.fragment_dialog.DialogFragmentHolder;
import com.yinp.tools.fragment_dialog.ViewConvertListener;
import com.yinp.tools.utils.DateUtils;

/**
 * 添加待做的任务，或者其他的什么东西
 */
public class AddUndeterminedActivity extends AppBaseActivity<ActivityAddUndeterminedBinding> {
    private DaoUtils<LocaleTaskBean> localeTaskBeanDaoUtils;

    @Override
    protected void initViews() {
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(mContext));
        initClick(bd.header.headerBackImg, bd.stvSave);
        bd.header.headerCenterTitle.setText("添加任务今日");

        localeTaskBeanDaoUtils = new DaoUtils<>(LocaleTaskBean.class, App.daoSession.getLocaleTaskBeanDao());
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == bd.header.headerBackImg) {
            finish();
        } else if (v == bd.stvSave) {
            saveDialog();
        }
    }

    private void insertData() {
        if (TextUtils.isEmpty(bd.etContent.getText())) {
            showToast("还没有填写内容");
            return;
        }
        LocaleTaskBean localeTaskBean = new LocaleTaskBean();
        localeTaskBean.setContent(bd.etContent.getText().toString().trim());
        localeTaskBean.setEmpirical(2);
        localeTaskBean.setDate(DateUtils.curData.curDate());
        localeTaskBean.setTime(DateUtils.curData.curTime());
        localeTaskBean.setUserId(1001L);
        if (!TextUtils.isEmpty(bd.etTitle.getText())) {
            localeTaskBean.setTitle(bd.etTitle.getText().toString().trim());
        }
        boolean isSuccess = localeTaskBeanDaoUtils.insert(localeTaskBean);
        if (isSuccess) {
            showToast("添加成功");
            setResult(Constant.RESULT_CODE);
            finish();
        } else {
            showToast("添加失败");
        }
    }

    private void saveDialog() {
        CommonDialogFragment.newInstance().setLayout(DialogTipBinding.inflate(LayoutInflater.from(mContext), null, false))
                .setViewConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(DialogFragmentHolder holder, BaseDialogFragment dialogFragment, ViewBinding viewBinding) {
                        super.convertView(holder, dialogFragment, viewBinding);
                        DialogTipBinding binding = (DialogTipBinding) viewBinding;
                        binding.tvTitle.setText("确定添加吗");
                        binding.tvLeft.setOnClickListener(v -> dialogFragment.dismiss());
                        binding.tvRight.setOnClickListener(v -> {
                            dialogFragment.dismiss();
                            insertData();
                        });
                    }
                }).setAnimStyle(BaseDialogFragment.CENTER).setGravity(BaseDialogFragment.CENTER).setPercentSize(0.9f, 0).show(getSupportFragmentManager());
    }
}